<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li class="active"><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. HTTP服务器">1. HTTP服务器</a><ul><li><a href="#t11.1 创建HTTP服务器">1.1 创建HTTP服务器</a></li><li><a href="#t21.2 启动HTTP服务器">1.2 启动HTTP服务器</a></li><li><a href="#t31.3 关闭HTTP服务器">1.3 关闭HTTP服务器</a></li><li><a href="#t41.4 监听服务器错误">1.4 监听服务器错误</a></li><li><a href="#t51.5 connection">1.5 connection</a></li><li><a href="#t61.6 setTimeout">1.6 setTimeout</a></li><li><a href="#t71.7 获取客户端请求信息">1.7 获取客户端请求信息</a></li><li><a href="#t81.8 querystring">1.8 querystring</a><ul><li><a href="#t91.8.1 parse方法用来把字符串转换成对象">1.8.1 parse方法用来把字符串转换成对象</a></li><li><a href="#t101.8.2 stringify方法用来把对象转换成字符串">1.8.2 stringify方法用来把对象转换成字符串</a></li></ul></li><li><a href="#t111.9 querystring">1.9 querystring</a></li><li><a href="#t121.10 发送服务器响应流">1.10 发送服务器响应流</a><ul><li><a href="#t131.10.1 writeHead">1.10.1 writeHead</a></li><li><a href="#t141.10.2 Header">1.10.2 Header</a></li><li><a href="#t151.10.3 headersSent">1.10.3 headersSent</a></li><li><a href="#t161.10.4 sendDate">1.10.4 sendDate</a></li><li><a href="#t171.10.5 write">1.10.5 write</a></li><li><a href="#t181.10.6 timeout">1.10.6 timeout</a></li><li><a href="#t191.10.7 close">1.10.7 close</a></li><li><a href="#t201.10.8 parser">1.10.8 parser</a></li></ul></li></ul></li><li><a href="#t212. HTTP客户端">2. HTTP客户端</a><ul><li><a href="#t222.1 向其他网站请求数据">2.1 向其他网站请求数据</a></li><li><a href="#t232.2 取消请求">2.2 取消请求</a></li><li><a href="#t242.3 监听error事件">2.3 监听error事件</a></li><li><a href="#t252.4 socket">2.4 socket</a></li><li><a href="#t262.5 get">2.5 get</a></li><li><a href="#t272.6 addTrailers">2.6 addTrailers</a></li><li><a href="#t282.7 制作代理服务器">2.7 制作代理服务器</a></li></ul></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. HTTP&#x670D;&#x52A1;&#x5668;">1. HTTP&#x670D;&#x52A1;&#x5668; <a href="#t01. HTTP&#x670D;&#x52A1;&#x5668;"> # </a></h2>
<p>HTTP&#x5168;&#x79F0;&#x662F;&#x8D85;&#x6587;&#x672C;&#x4F20;&#x8F93;&#x534F;&#x8BAE;&#xFF0C;&#x6784;&#x5EFA;&#x4E8E;TCP&#x4E4B;&#x4E0A;&#xFF0C;&#x5C5E;&#x4E8E;&#x5E94;&#x7528;&#x5C42;&#x534F;&#x8BAE;&#x3002;</p>
<h3 id="t11.1 &#x521B;&#x5EFA;HTTP&#x670D;&#x52A1;&#x5668;">1.1 &#x521B;&#x5EFA;HTTP&#x670D;&#x52A1;&#x5668; <a href="#t11.1 &#x521B;&#x5EFA;HTTP&#x670D;&#x52A1;&#x5668;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> server  = http.createServer([requestListener]);
server.on(<span class="hljs-string">&apos;request&apos;</span>,requestListener);
</code></pre>
<ul>
<li>requestListener &#x5F53;&#x670D;&#x52A1;&#x5668;&#x6536;&#x5230;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8FDE;&#x63A5;&#x540E;&#x6267;&#x884C;&#x7684;&#x5904;&#x7406;<ul>
<li>http.IncomingMessage &#x8BF7;&#x6C42;&#x5BF9;&#x8C61;</li>
<li>http.ServerResponse&#x5BF9;&#x8C61; &#x670D;&#x52A1;&#x5668;&#x7AEF;&#x54CD;&#x5E94;&#x5BF9;&#x8C61;</li>
</ul>
</li>
</ul>
<h3 id="t21.2 &#x542F;&#x52A8;HTTP&#x670D;&#x52A1;&#x5668;">1.2 &#x542F;&#x52A8;HTTP&#x670D;&#x52A1;&#x5668; <a href="#t21.2 &#x542F;&#x52A8;HTTP&#x670D;&#x52A1;&#x5668;"> # </a></h3>
<pre><code class="lang-javascript">server.listen(port,[host],[backlog],[callback]);
server.on(<span class="hljs-string">&apos;listening&apos;</span>,callback);
</code></pre>
<ul>
<li>port &#x76D1;&#x542C;&#x7684;&#x7AEF;&#x53E3;&#x53F7;</li>
<li>host &#x76D1;&#x542C;&#x7684;&#x5730;&#x5740;</li>
<li>backlog &#x6307;&#x5B9A;&#x4F4D;&#x4E8E;&#x7B49;&#x5F85;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x6570;</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">let</span> server = http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
}).listen(<span class="hljs-number">8080</span>,<span class="hljs-string">&apos;127.0.0.1&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x5F00;&#x59CB;&#x76D1;&#x542C;!&apos;</span>)});
</code></pre>
<h3 id="t31.3 &#x5173;&#x95ED;HTTP&#x670D;&#x52A1;&#x5668;">1.3 &#x5173;&#x95ED;HTTP&#x670D;&#x52A1;&#x5668; <a href="#t31.3 &#x5173;&#x95ED;HTTP&#x670D;&#x52A1;&#x5668;"> # </a></h3>
<pre><code class="lang-javascript">server.close();
server.on(<span class="hljs-string">&apos;close&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{});
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">let</span> server = http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
});
server.on(<span class="hljs-string">&apos;close&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x670D;&#x52A1;&#x5668;&#x5173;&#x95ED;&apos;</span>);
});
server.listen(<span class="hljs-number">8080</span>,<span class="hljs-string">&apos;127.0.0.1&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x5F00;&#x59CB;&#x76D1;&#x542C;!&apos;</span>)
    server.close();
});
</code></pre>
<h3 id="t41.4 &#x76D1;&#x542C;&#x670D;&#x52A1;&#x5668;&#x9519;&#x8BEF;">1.4 &#x76D1;&#x542C;&#x670D;&#x52A1;&#x5668;&#x9519;&#x8BEF; <a href="#t41.4 &#x76D1;&#x542C;&#x670D;&#x52A1;&#x5668;&#x9519;&#x8BEF;"> # </a></h3>
<pre><code class="lang-javascript">server.on(&apos;error&apos;,function(){
    if(e.code == &apos;EADDRINUSE&apos;){
         console.log(&apos;&#x7AEF;&#x53E3;&#x53F7;&#x5DF2;&#x7ECF;&#x88AB;&#x5360;&#x7528;!);   
    }
});
</code></pre>
<h3 id="t51.5 connection">1.5 connection <a href="#t51.5 connection"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> server = http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
});
server.on(<span class="hljs-string">&apos;connection&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-built_in">console</span>.log(&#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x5DF2;&#x7ECF;&#x5EFA;&#x7ACB;);
});
</code></pre>
<h3 id="t61.6 setTimeout">1.6 setTimeout <a href="#t61.6 setTimeout"> # </a></h3>
<p>&#x8BBE;&#x7F6E;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#xFF0C;&#x8D85;&#x65F6;&#x540E;&#x4E0D;&#x53EF;&#x518D;&#x590D;&#x7528;&#x5DF2;&#x7ECF;&#x5EFA;&#x7ACB;&#x7684;&#x8FDE;&#x63A5;&#xFF0C;&#x9700;&#x8981;&#x53D1;&#x8BF7;&#x6C42;&#x9700;&#x8981;&#x91CD;&#x65B0;&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;&#x3002;&#x9ED8;&#x8BA4;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#x65F6;2&#x5206;&#x949F;</p>
<pre><code class="lang-javascript">server.setTimeout(msecs,callback);
server.on(<span class="hljs-string">&apos;timeout&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x8FDE;&#x63A5;&#x5DF2;&#x7ECF;&#x8D85;&#x65F6;&apos;</span>);
});
</code></pre>
<h3 id="t71.7 &#x83B7;&#x53D6;&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x4FE1;&#x606F;">1.7 &#x83B7;&#x53D6;&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x4FE1;&#x606F; <a href="#t71.7 &#x83B7;&#x53D6;&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x4FE1;&#x606F;"> # </a></h3>
<ul>
<li>request<ul>
<li>method &#x8BF7;&#x6C42;&#x7684;&#x65B9;&#x6CD5;</li>
<li>url &#x8BF7;&#x6C42;&#x7684;&#x8DEF;&#x5F84;</li>
<li>headers &#x8BF7;&#x6C42;&#x5934;&#x5BF9;&#x8C61;</li>
<li>httpVersion &#x5BA2;&#x6237;&#x7AEF;&#x7684;http&#x7248;&#x672C;</li>
<li>socket &#x76D1;&#x542C;&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x7684;socket&#x5BF9;&#x8C61;<pre><code class="lang-javascript">let http = require(&apos;http&apos;);
let fs = require(&apos;fs&apos;);
let server = http.createServer(function(req,res){
if(req.url != &apos;/favicon.ico&apos;){
let out = fs.createWriteStream(path.join(__dirname,&apos;request.log&apos;));
out.write(&apos;method=&apos;+req.method);
out.write(&apos;url=&apos;+req.url);
out.write(&apos;headers=&apos;+JSON.stringify(req.headers));
out.write(&apos;httpVersion=&apos;+req.httpVersion);
}
}).listen(8080,&apos;127.0.0.1);
</code></pre>
</li>
</ul>
</li>
</ul>
<pre><code class="lang-javascript">let http = require(&apos;http&apos;);
let fs = require(&apos;fs&apos;);
let server = http.createServer(function(req,res){
  let body = [];
  req.on(&apos;data&apos;,function(data){
    body.push(data);
  });
  req.on(&apos;end&apos;,function(){
      let result = Buffer.concat(body);
      console.log(result.toString());
  });
}).listen(8080,&apos;127.0.0.1);
</code></pre>
<h3 id="t81.8 querystring">1.8 querystring <a href="#t81.8 querystring"> # </a></h3>
<p>querystring&#x6A21;&#x5757;&#x7528;&#x6765;&#x8F6C;&#x6362;URL&#x5B57;&#x7B26;&#x4E32;&#x548C;URL&#x4E2D;&#x7684;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;</p>
<h4 id="t91.8.1 parse&#x65B9;&#x6CD5;&#x7528;&#x6765;&#x628A;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210;&#x5BF9;&#x8C61;">1.8.1 parse&#x65B9;&#x6CD5;&#x7528;&#x6765;&#x628A;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210;&#x5BF9;&#x8C61; <a href="#t91.8.1 parse&#x65B9;&#x6CD5;&#x7528;&#x6765;&#x628A;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210;&#x5BF9;&#x8C61;"> # </a></h4>
<pre><code class="lang-javascript">querystring.parse(str,[sep],[eq],[options]);
</code></pre>
<h4 id="t101.8.2 stringify&#x65B9;&#x6CD5;&#x7528;&#x6765;&#x628A;&#x5BF9;&#x8C61;&#x8F6C;&#x6362;&#x6210;&#x5B57;&#x7B26;&#x4E32;">1.8.2 stringify&#x65B9;&#x6CD5;&#x7528;&#x6765;&#x628A;&#x5BF9;&#x8C61;&#x8F6C;&#x6362;&#x6210;&#x5B57;&#x7B26;&#x4E32; <a href="#t101.8.2 stringify&#x65B9;&#x6CD5;&#x7528;&#x6765;&#x628A;&#x5BF9;&#x8C61;&#x8F6C;&#x6362;&#x6210;&#x5B57;&#x7B26;&#x4E32;"> # </a></h4>
<pre><code class="lang-javascript">querystring.stringify(obj,[sep],[eq]);
</code></pre>
<h3 id="t111.9 querystring">1.9 querystring <a href="#t111.9 querystring"> # </a></h3>
<pre><code class="lang-javascript">url.parse(urlStr,[parseQueryString]);
</code></pre>
<ul>
<li>href &#x88AB;&#x8F6C;&#x6362;&#x7684;&#x539F;URL&#x5B57;&#x7B26;&#x4E32;</li>
<li>protocal &#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x51FA;&#x8BF7;&#x6C42;&#x65F6;&#x4F7F;&#x7528;&#x7684;&#x534F;&#x8BAE;</li>
<li>slashes &#x5728;&#x534F;&#x8BAE;&#x4E0E;&#x8DEF;&#x5F84;&#x4E4B;&#x95F4;&#x662F;&#x5426;&#x4F7F;&#x7528;&#x4E86;//&#x5206;&#x9694;&#x7B26;</li>
<li>host URL&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x5B8C;&#x6574;&#x5730;&#x5740;&#x548C;&#x7AEF;&#x53E3;&#x53F7;</li>
<li>auth URL&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x8BA4;&#x8BC1;&#x90E8;&#x5206;</li>
<li>hostname URL&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x5B8C;&#x6574;&#x5730;&#x5740;</li>
<li>port URL&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x7AEF;&#x53E3;&#x53F7;</li>
<li>pathname URL&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x8DEF;&#x5F84;&#xFF0C;&#x4E0D;&#x5305;&#x542B;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;</li>
<li>search &#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5305;&#x542B;?</li>
<li>path &#x8DEF;&#x5F84;&#xFF0C;&#x5305;&#x542B;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;</li>
<li>query &#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x4E0D;&#x5305;&#x542B;&#x8D77;&#x59CB;&#x5B57;&#x7B26;&#x4E32;<code>?</code></li>
<li>hash &#x6563;&#x5217;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5305;&#x542B;<code>#</code></li>
</ul>
<h3 id="t121.10 &#x53D1;&#x9001;&#x670D;&#x52A1;&#x5668;&#x54CD;&#x5E94;&#x6D41;">1.10 &#x53D1;&#x9001;&#x670D;&#x52A1;&#x5668;&#x54CD;&#x5E94;&#x6D41; <a href="#t121.10 &#x53D1;&#x9001;&#x670D;&#x52A1;&#x5668;&#x54CD;&#x5E94;&#x6D41;"> # </a></h3>
<p>http.ServerResponse&#x5BF9;&#x8C61;&#x8868;&#x793A;&#x54CD;&#x5E94;&#x5BF9;&#x8C61;</p>
<h4 id="t131.10.1 writeHead">1.10.1 writeHead <a href="#t131.10.1 writeHead"> # </a></h4>
<pre><code class="lang-javascript">response.writeHead(statusCode,[reasonPhrase],[headers]);
</code></pre>
<ul>
<li>content-type  &#x5185;&#x5BB9;&#x7C7B;&#x578B;</li>
<li>location &#x5C06;&#x5BA2;&#x6237;&#x7AEF;&#x91CD;&#x5B9A;&#x5411;&#x5230;&#x53E6;&#x5916;&#x4E00;&#x4E2A;URL&#x5730;&#x5740;</li>
<li>content-disposition &#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x88AB;&#x4E0B;&#x8F7D;&#x7684;&#x6587;&#x4EF6;&#x540D;</li>
<li>content-length &#x670D;&#x52A1;&#x5668;&#x54CD;&#x5E94;&#x5185;&#x5BB9;&#x7684;&#x5B57;&#x8282;&#x6570;</li>
<li>set-cookie &#x5728;&#x5BA2;&#x6237;&#x7AEF;&#x521B;&#x5EFA;Cookie</li>
<li>content-encoding &#x6307;&#x5B9A;&#x670D;&#x52A1;&#x5668;&#x54CD;&#x5E94;&#x5185;&#x5BB9;&#x7684;&#x7F16;&#x7801;&#x65B9;&#x5F0F;</li>
<li>cache-cache  &#x5F00;&#x542F;&#x7F13;&#x5B58;&#x673A;&#x5236;</li>
<li>expires &#x7528;&#x4E8E;&#x5236;&#x5B9A;&#x7F13;&#x5B58;&#x8FC7;&#x671F;&#x65F6;&#x95F4;</li>
<li>etag &#x6307;&#x5B9A;&#x5F53;&#x670D;&#x52A1;&#x5668;&#x54CD;&#x5E94;&#x5185;&#x5BB9;&#x6CA1;&#x6709;&#x53D8;&#x5316;&#x4E0D;&#x91CD;&#x65B0;&#x4E0B;&#x8F7D;&#x6570;&#x636E;</li>
</ul>
<h4 id="t141.10.2 Header">1.10.2 Header <a href="#t141.10.2 Header"> # </a></h4>
<p>&#x8BBE;&#x7F6E;&#x3001;&#x83B7;&#x53D6;&#x548C;&#x5220;&#x9664;Header</p>
<pre><code class="lang-javascript">response.setHeader(<span class="hljs-string">&apos;Content-Type&apos;</span>,<span class="hljs-string">&apos;text/html;charset=utf-8&apos;</span>);
response.getHeader(<span class="hljs-string">&apos;Content-Type&apos;</span>);
response.removeHeader(<span class="hljs-string">&apos;Content-Type&apos;</span>);
response.headersSent &#x5224;&#x65AD;&#x54CD;&#x5E94;&#x5934;&#x662F;&#x5426;&#x5DF2;&#x7ECF;&#x53D1;&#x9001;
</code></pre>
<h4 id="t151.10.3 headersSent">1.10.3 headersSent <a href="#t151.10.3 headersSent"> # </a></h4>
<p>&#x5224;&#x65AD;&#x54CD;&#x5E94;&#x5934;&#x662F;&#x5426;&#x5DF2;&#x7ECF;&#x53D1;&#x9001;</p>
<pre><code class="lang-javascript">let http = require(&apos;http&apos;);
let server = http.createServer(function(req,res){
  console.log(resopnse.headersSent?&quot;&#x54CD;&#x5E94;&#x5934;&#x5DF2;&#x7ECF;&#x53D1;&#x9001;&quot;:&quot;&#x54CD;&#x5E94;&#x5934;&#x672A;&#x53D1;&#x9001;!&quot;);
  res.writeHead(200,&apos;ok);
  console.log(resopnse.headersSent?&quot;&#x54CD;&#x5E94;&#x5934;&#x5DF2;&#x7ECF;&#x53D1;&#x9001;&quot;:&quot;&#x54CD;&#x5E94;&#x5934;&#x672A;&#x53D1;&#x9001;!&quot;);
});
</code></pre>
<h4 id="t161.10.4 sendDate">1.10.4 sendDate <a href="#t161.10.4 sendDate"> # </a></h4>
<p>&#x4E0D;&#x53D1;&#x9001;Date</p>
<pre><code class="lang-javascript">res.sendDate = <span class="hljs-literal">false</span>;
</code></pre>
<h4 id="t171.10.5 write">1.10.5 write <a href="#t171.10.5 write"> # </a></h4>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;write&#x65B9;&#x6CD5;&#x53D1;&#x9001;&#x54CD;&#x5E94;&#x5185;&#x5BB9;</p>
<pre><code class="lang-javascript">response.write(chunk,[encoding]);
response.end([chunk],[encoding]);
</code></pre>
<h4 id="t181.10.6 timeout">1.10.6 timeout <a href="#t181.10.6 timeout"> # </a></h4>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;setTimeout&#x65B9;&#x6CD5;&#x8BBE;&#x7F6E;&#x54CD;&#x5E94;&#x8BA9;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#xFF0C;&#x5982;&#x679C;&#x5728;&#x6307;&#x5B9A;&#x65F6;&#x95F4;&#x5185;&#x4E0D;&#x54CD;&#x5E94;&#xFF0C;&#x5219;&#x89E6;&#x53D1;timeout&#x4E8B;&#x4EF6;</p>
<pre><code class="lang-javascript">response.setTimeout(msecs,[callback]);
response.on(<span class="hljs-string">&apos;timeout&apos;</span>,callback);
</code></pre>
<h4 id="t191.10.7 close">1.10.7 close <a href="#t191.10.7 close"> # </a></h4>
<p>&#x5728;&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#x7684;end&#x65B9;&#x6CD5;&#x88AB;&#x8C03;&#x7528;&#x4E4B;&#x524D;&#xFF0C;&#x5982;&#x679C;&#x8FDE;&#x63A5;&#x4E2D;&#x65AD;&#xFF0C;&#x5C06;&#x89E6;&#x53D1;http.ServerResponse&#x5BF9;&#x8C61;&#x7684;close&#x4E8B;&#x4EF6;</p>
<pre><code class="lang-javascript">response.on(<span class="hljs-string">&apos;close&apos;</span>,callback);
</code></pre>
<h4 id="t201.10.8 parser">1.10.8 parser <a href="#t201.10.8 parser"> # </a></h4>
<pre><code class="lang-javascript">net
onconnection

_http_server.js
&#x8FDE;&#x63A5;&#x76D1;&#x542C;
connectionListenerInternal
socketOnData
onParserExecuteCommon
parserOnIncoming


</code></pre>
<h2 id="t212. HTTP&#x5BA2;&#x6237;&#x7AEF;">2. HTTP&#x5BA2;&#x6237;&#x7AEF; <a href="#t212. HTTP&#x5BA2;&#x6237;&#x7AEF;"> # </a></h2>
<h3 id="t222.1 &#x5411;&#x5176;&#x4ED6;&#x7F51;&#x7AD9;&#x8BF7;&#x6C42;&#x6570;&#x636E;">2.1 &#x5411;&#x5176;&#x4ED6;&#x7F51;&#x7AD9;&#x8BF7;&#x6C42;&#x6570;&#x636E; <a href="#t222.1 &#x5411;&#x5176;&#x4ED6;&#x7F51;&#x7AD9;&#x8BF7;&#x6C42;&#x6570;&#x636E;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> req = http.request(options,callback);
req.on(<span class="hljs-string">&apos;request&apos;</span>,callback);
request.write(chunk,[encoding]);
request.end([chunk],[encoding]);

</code></pre>
<ul>
<li>host &#x6307;&#x5B9A;&#x76EE;&#x6807;&#x57DF;&#x540D;&#x6216;&#x4E3B;&#x673A;&#x540D;</li>
<li>hostname &#x6307;&#x5B9A;&#x76EE;&#x6807;&#x57DF;&#x540D;&#x6216;&#x4E3B;&#x673A;&#x540D;&#xFF0C;&#x5982;&#x679C;&#x548C;host&#x90FD;&#x6307;&#x5B9A;&#x4E86;&#xFF0C;&#x4F18;&#x5148;&#x4F7F;&#x7528;hostname</li>
<li>port &#x6307;&#x5B9A;&#x76EE;&#x6807;&#x670D;&#x52A1;&#x5668;&#x7684;&#x7AEF;&#x53E3;&#x53F7;</li>
<li>localAddress &#x672C;&#x5730;&#x63A5;&#x53E3;</li>
<li>socketPath &#x6307;&#x5B9A;Unix&#x57DF;&#x7AEF;&#x53E3;</li>
<li>method &#x6307;&#x5B9A;HTTP&#x8BF7;&#x6C42;&#x7684;&#x65B9;&#x5F0F;</li>
<li>path &#x6307;&#x5B9A;&#x8BF7;&#x6C42;&#x8DEF;&#x5F84;&#x548C;&#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;</li>
<li>headers &#x6307;&#x5B9A;&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x5934;&#x5BF9;&#x8C61;</li>
<li>auth &#x6307;&#x5B9A;&#x8BA4;&#x8BC1;&#x90E8;&#x5206;</li>
<li>agent &#x7528;&#x4E8E;&#x6307;&#x5B9A;HTTP&#x4EE3;&#x7406;&#xFF0C;&#x5728;Node.js&#x4E2D;&#xFF0C;&#x4F7F;&#x7528;http.Agent&#x7C7B;&#x4EE3;&#x8868;&#x4E00;&#x4E2A;HTTP&#x4EE3;&#x7406;&#xFF0C;&#x9ED8;&#x8BA4;&#x4F7F;&#x7528;keep-alive&#x8FDE;&#x63A5;&#xFF0C;&#x540C;&#x65F6;&#x4F7F;&#x7528;http.Agent&#x5BF9;&#x8C61;&#x6765;&#x5B9E;&#x73B0;&#x6240;&#x6709;&#x7684;HTTP&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">let</span> options = {
    <span class="hljs-attr">hostname</span>: <span class="hljs-string">&apos;localhost&apos;</span>,
    <span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>,
    <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/&apos;</span>,
    <span class="hljs-attr">method</span>: <span class="hljs-string">&apos;GET&apos;</span>
}
<span class="hljs-keyword">let</span> req = http.request(options, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">res</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x72B6;&#x6001;&#x5417;:&apos;</span> + res.statusCode);
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x54CD;&#x5E94;&#x5934;:&apos;</span> + <span class="hljs-built_in">JSON</span>.stringify(res.headers));
    res.setEncoding(<span class="hljs-string">&apos;utf8&apos;</span>);
    res.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">chunk</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x54CD;&#x5E94;&#x5185;&#x5BB9;&apos;</span>, chunk);
    });
});
req.end();
</code></pre>
<h3 id="t232.2 &#x53D6;&#x6D88;&#x8BF7;&#x6C42;">2.2 &#x53D6;&#x6D88;&#x8BF7;&#x6C42; <a href="#t232.2 &#x53D6;&#x6D88;&#x8BF7;&#x6C42;"> # </a></h3>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;abort&#x65B9;&#x6CD5;&#x6765;&#x7EC8;&#x6B62;&#x672C;&#x6B21;&#x8BF7;&#x6C42;</p>
<pre><code class="lang-javascript">req.abort();
</code></pre>
<h3 id="t242.3 &#x76D1;&#x542C;error&#x4E8B;&#x4EF6;">2.3 &#x76D1;&#x542C;error&#x4E8B;&#x4EF6; <a href="#t242.3 &#x76D1;&#x542C;error&#x4E8B;&#x4EF6;"> # </a></h3>
<p>&#x5982;&#x679C;&#x8BF7;&#x6C42;&#x8FC7;&#x7A0B;&#x4E2D;&#x51FA;&#x9519;&#x4E86;&#xFF0C;&#x4F1A;&#x89E6;&#x53D1;error&#x4E8B;&#x4EF6;</p>
<pre><code class="lang-javascripot">request.on(&apos;error&apos;,function(err){});
</code></pre>
<h3 id="t252.4 socket">2.4 socket <a href="#t252.4 socket"> # </a></h3>
<p>&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x4E3A;&#x8BE5;&#x8FDE;&#x63A5;&#x5206;&#x914D;&#x7AEF;&#x53E3;&#x65F6;&#xFF0C;&#x89E6;&#x53D1;<code>socket</code>&#x4E8B;&#x4EF6;</p>
<pre><code class="lang-javascript">req.on(<span class="hljs-string">&apos;socket&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">socket</span>)</span>{
  socket.setTimeout(<span class="hljs-number">1000</span>);
  socket.on(<span class="hljs-string">&apos;timeout&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{req.abort()});
});
</code></pre>
<h3 id="t262.5 get">2.5 get <a href="#t262.5 get"> # </a></h3>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;get&#x65B9;&#x6CD5;&#x5411;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x9001;&#x6570;&#x636E;</p>
<pre><code class="lang-javascript">http.get(options,callback);
</code></pre>
<h3 id="t272.6 addTrailers">2.6 addTrailers <a href="#t272.6 addTrailers"> # </a></h3>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;response&#x5BF9;&#x8C61;&#x7684;addTrailers&#x65B9;&#x6CD5;&#x5728;&#x670D;&#x52A1;&#x5668;&#x54CD;&#x5E94;&#x5C3E;&#x90E8;&#x8FFD;&#x52A0;&#x4E00;&#x4E2A;&#x5934;&#x4FE1;&#x606F;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> crypto = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;crypto&apos;</span>);


<span class="hljs-keyword">let</span> server = http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
    res.writeHead(<span class="hljs-number">200</span>, {
        <span class="hljs-string">&apos;Transfer-Encoding&apos;</span>: <span class="hljs-string">&apos;chunked&apos;</span>,
        <span class="hljs-string">&apos;Trailer&apos;</span>: <span class="hljs-string">&apos;Content-MD5&apos;</span>
    });
    <span class="hljs-keyword">let</span> rs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>).createReadStream(path.join(__dirname, <span class="hljs-string">&apos;msg.txt&apos;</span>), {
        <span class="hljs-attr">highWaterMark</span>: <span class="hljs-number">2</span>
    });
    <span class="hljs-keyword">let</span> md5 = crypto.createHash(<span class="hljs-string">&apos;md5&apos;</span>);
    rs.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
        <span class="hljs-built_in">console</span>.log(data);
        res.write(data);
        md5.update(data);
    });
    rs.on(<span class="hljs-string">&apos;end&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        res.addTrailers({
            <span class="hljs-string">&apos;Content-MD5&apos;</span>: md5.digest(<span class="hljs-string">&apos;hex&apos;</span>)
        });
        res.end();
    });
}).listen(<span class="hljs-number">8080</span>);
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">let</span> options = {
    <span class="hljs-attr">hostname</span>: <span class="hljs-string">&apos;localhost&apos;</span>,
    <span class="hljs-attr">port</span>: <span class="hljs-number">8080</span>,
    <span class="hljs-attr">path</span>: <span class="hljs-string">&apos;/&apos;</span>,
    <span class="hljs-attr">method</span>: <span class="hljs-string">&apos;GET&apos;</span>
}
<span class="hljs-keyword">let</span> req = http.request(options, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">res</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x72B6;&#x6001;&#x5417;:&apos;</span> + res.statusCode);
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x54CD;&#x5E94;&#x5934;:&apos;</span> + <span class="hljs-built_in">JSON</span>.stringify(res.headers));
    res.setEncoding(<span class="hljs-string">&apos;utf8&apos;</span>);
    res.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">chunk</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x54CD;&#x5E94;&#x5185;&#x5BB9;&apos;</span>, chunk);
    });
    res.on(<span class="hljs-string">&apos;end&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;trailer&apos;</span>, res.trailers);
    });
});
req.end();
</code></pre>
<h3 id="t282.7 &#x5236;&#x4F5C;&#x4EE3;&#x7406;&#x670D;&#x52A1;&#x5668;">2.7 &#x5236;&#x4F5C;&#x4EE3;&#x7406;&#x670D;&#x52A1;&#x5668; <a href="#t282.7 &#x5236;&#x4F5C;&#x4EE3;&#x7406;&#x670D;&#x52A1;&#x5668;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> http = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;http&apos;</span>);
<span class="hljs-keyword">let</span> url = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;url&apos;</span>);
<span class="hljs-keyword">let</span> server = http.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">request, response</span>) </span>{
    <span class="hljs-keyword">let</span> {
        path
    } = url.parse(request.url);
    <span class="hljs-keyword">let</span> options = {
        <span class="hljs-attr">host</span>: <span class="hljs-string">&apos;localhost&apos;</span>,
        <span class="hljs-attr">port</span>: <span class="hljs-number">9090</span>,
        <span class="hljs-attr">path</span>: path,
        <span class="hljs-attr">headers</span>: request.headers
    }
    <span class="hljs-keyword">let</span> req = http.get(options, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">res</span>) </span>{
        <span class="hljs-built_in">console</span>.log(res);
        response.writeHead(res.statusCode, res.headers);
        res.pipe(response);
    });
    req.on(<span class="hljs-string">&apos;error&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
        <span class="hljs-built_in">console</span>.log(err);
    });
    request.pipe(req);
}).listen(<span class="hljs-number">8080</span>);
</code></pre>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
